{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c2a901b2-e116-4ae3-9d29-d6a9aeaa590c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: langchain in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (0.3.26)\n",
      "Requirement already satisfied: langchain-core<1.0.0,>=0.3.66 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (0.3.67)\n",
      "Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.8 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (0.3.8)\n",
      "Requirement already satisfied: langsmith>=0.1.17 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (0.4.4)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (2.10.3)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (2.0.41)\n",
      "Requirement already satisfied: requests<3,>=2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (2.32.4)\n",
      "Requirement already satisfied: PyYAML>=5.3 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain) (6.0.2)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (9.1.2)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (1.33)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (4.12.2)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.66->langchain) (3.0.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.27.1)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain) (3.4.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain) (2.4.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain) (2025.4.26)\n",
      "Requirement already satisfied: greenlet>=1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.2.3)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.17->langchain) (0.28.1)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.17->langchain) (3.10.18)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.17->langchain) (1.0.0)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.17->langchain) (0.23.0)\n",
      "Requirement already satisfied: anyio in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (4.7.0)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (0.16.0)\n",
      "Requirement already satisfied: sniffio>=1.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (1.3.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eef90613-8486-4f40-ae91-2a85367f3c67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting langchain-openai\n",
      "  Downloading langchain_openai-0.3.27-py3-none-any.whl.metadata (2.3 kB)\n",
      "Requirement already satisfied: langchain-core<1.0.0,>=0.3.66 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-openai) (0.3.67)\n",
      "Collecting openai<2.0.0,>=1.86.0 (from langchain-openai)\n",
      "  Downloading openai-1.93.0-py3-none-any.whl.metadata (29 kB)\n",
      "Collecting tiktoken<1,>=0.7 (from langchain-openai)\n",
      "  Using cached tiktoken-0.9.0-cp313-cp313-win_amd64.whl.metadata (6.8 kB)\n",
      "Requirement already satisfied: langsmith>=0.3.45 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (0.4.4)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (9.1.2)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (1.33)\n",
      "Requirement already satisfied: PyYAML>=5.3 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (6.0.2)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (4.12.2)\n",
      "Requirement already satisfied: pydantic>=2.7.4 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain-openai) (2.10.3)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.66->langchain-openai) (3.0.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (4.7.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (0.6.1)\n",
      "Requirement already satisfied: sniffio in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (1.3.0)\n",
      "Requirement already satisfied: tqdm>4 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (4.67.1)\n",
      "Requirement already satisfied: idna>=2.8 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.86.0->langchain-openai) (3.7)\n",
      "Requirement already satisfied: certifi in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain-openai) (2025.4.26)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain-openai) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain-openai) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.66->langchain-openai) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.66->langchain-openai) (2.27.1)\n",
      "Collecting regex>=2022.1.18 (from tiktoken<1,>=0.7->langchain-openai)\n",
      "  Using cached regex-2024.11.6-cp313-cp313-win_amd64.whl.metadata (41 kB)\n",
      "Requirement already satisfied: requests>=2.26.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from tiktoken<1,>=0.7->langchain-openai) (2.32.4)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.66->langchain-openai) (3.10.18)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.66->langchain-openai) (1.0.0)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.66->langchain-openai) (0.23.0)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai) (3.4.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai) (2.4.0)\n",
      "Requirement already satisfied: colorama in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from tqdm>4->openai<2.0.0,>=1.86.0->langchain-openai) (0.4.6)\n",
      "Downloading langchain_openai-0.3.27-py3-none-any.whl (70 kB)\n",
      "Downloading openai-1.93.0-py3-none-any.whl (755 kB)\n",
      "   ---------------------------------------- 0.0/755.0 kB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/755.0 kB ? eta -:--:--\n",
      "   ------------- -------------------------- 262.1/755.0 kB ? eta -:--:--\n",
      "   ---------------------------------------- 755.0/755.0 kB 2.0 MB/s eta 0:00:00\n",
      "Using cached tiktoken-0.9.0-cp313-cp313-win_amd64.whl (894 kB)\n",
      "Using cached regex-2024.11.6-cp313-cp313-win_amd64.whl (273 kB)\n",
      "Installing collected packages: regex, tiktoken, openai, langchain-openai\n",
      "\n",
      "   ---------- ----------------------------- 1/4 [tiktoken]\n",
      "  Attempting uninstall: openai\n",
      "   ---------- ----------------------------- 1/4 [tiktoken]\n",
      "    Found existing installation: openai 1.82.1\n",
      "   ---------- ----------------------------- 1/4 [tiktoken]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "    Uninstalling openai-1.82.1:\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "      Successfully uninstalled openai-1.82.1\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   -------------------- ------------------- 2/4 [openai]\n",
      "   ------------------------------ --------- 3/4 [langchain-openai]\n",
      "   ---------------------------------------- 4/4 [langchain-openai]\n",
      "\n",
      "Successfully installed langchain-openai-0.3.27 openai-1.93.0 regex-2024.11.6 tiktoken-0.9.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  WARNING: The script openai.exe is installed in 'D:\\Users\\Lenovo\\anaconda3\\envs\\LLM\\Scripts' which is not on PATH.\n",
      "  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\n"
     ]
    }
   ],
   "source": [
    "pip install langchain-openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cffcd566-857e-4458-93dd-52c1289ccd4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "api_url=\"https://api.zetatechs.com/v1\"\n",
    "api_key=\"sk-TdHgyILmI69vVZ9S885unvFRqOX5Nil9jCDKg5YC0gucA2EJ\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d0f724e9-7652-4f6f-a5af-c7e4d434cd60",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b0d11d08-33e5-410c-8cb9-cb91df5d338b",
   "metadata": {},
   "outputs": [],
   "source": [
    "model=ChatOpenAI(model=\"gpt-3.5-turbo-16k\",base_url=api_url,api_key=api_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8582dc54-540e-46f4-b452-24a45c78f6fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='Hello, I am Jack.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 32, 'total_tokens': 38, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_0165350fbb', 'id': 'chatcmpl-BpA0A2Hwdhhc9f9sI8QLH0DUA3MRv', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--b201effd-d82b-410d-ae01-7bf31b8933c4-0', usage_metadata={'input_tokens': 32, 'output_tokens': 6, 'total_tokens': 38, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage, SystemMessage\n",
    "messages = [\n",
    "    SystemMessage(content=\"把中文翻译成英文\"),\n",
    "    HumanMessage(content=\"你好，我是杰克\"),\n",
    "]\n",
    "model.invoke(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "02eb7e7f-fef5-49e1-8da4-5ca502f85ab6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, I am Jack.\n",
      "{'refusal': None}\n"
     ]
    }
   ],
   "source": [
    "result=model.invoke(messages)\n",
    "print(result.content)\n",
    "print(result.additional_kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cf41b1bd-d903-4be2-8063-1712a5eb00f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "\n",
    "parser = StrOutputParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "40d1811c-50d0-483c-aab9-41d8d18201dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, I am Jack.'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parser.invoke(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bb998190-6e3e-416e-b052-1a2fd7d9daf9",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain=model|parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3da01171-7198-45b0-8a6b-342f2e01e630",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, I am Jack.'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bbcf9563-9ee1-46c4-a400-4748dd2ce07f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "67f65dbe-7641-4096-8c00-bff1504bb9c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_template = \"Translate the following into {language}:\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4a164dc2-da76-46bc-a122-a05bcf2da3de",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [(\"system\", system_template), (\"user\", \"{text}\")]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ab08bf44-355f-4687-b125-9d66f9f28283",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='Translate the following into italian:', additional_kwargs={}, response_metadata={}), HumanMessage(content='hi', additional_kwargs={}, response_metadata={})])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = prompt_template.invoke({\"language\": \"italian\", \"text\": \"hi\"})\n",
    "\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e50973dc-e583-4e4a-9f7c-7cc582da8c11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content='Translate the following into italian:', additional_kwargs={}, response_metadata={}),\n",
       " HumanMessage(content='hi', additional_kwargs={}, response_metadata={})]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "336dc617-1a66-4f56-a4c0-fcb86ef6b4e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='Ciao', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 2, 'prompt_tokens': 18, 'total_tokens': 20, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_0165350fbb', 'id': 'chatcmpl-BonzSgjbp5bXbxepFQlX302D6qkCz', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--903dd754-c298-41ee-97c5-9c4abe4d6908-0', usage_metadata={'input_tokens': 18, 'output_tokens': 2, 'total_tokens': 20, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.invoke(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80785c7d-5d66-4434-84f4-706db2142220",
   "metadata": {},
   "outputs": [],
   "source": [
    "parser.invoke(model.invoke(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "990588be-1fba-4ceb-a82d-185a0cf1eb0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Ciao'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parser.invoke(model.invoke(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26331ad0-8f5a-4c79-b842-c4022a5d2266",
   "metadata": {},
   "source": [
    "将prompt_template和model和parse关联起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "06ccc680-7e15-4d52-94ae-21f6a8e54939",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = prompt_template | model | parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2d372e2d-09d9-40a3-8d64-f937c92b31e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Mi piaci'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"language\": \"italian\", \"text\": \"我喜欢你\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "LLM",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
